package com.timber.util;


import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import com.borland.jbcl.layout.VerticalFlowLayout;


public class SimpleWaitDialog extends JDialog
{
  protected JPanel		mainPanel = new JPanel();
  protected BorderLayout	borderLayout1 = new BorderLayout();
  protected JPanel		topPanel = new JPanel();
  protected FlowLayout		flowLayout1 = new FlowLayout();
  protected JLabel		topLabel = new JLabel();
  protected JPanel		notePanel = new JPanel();
  protected JLabel		noteLabel = new JLabel();
  protected JLabel		gifLabel = new JLabel();
  protected JPanel		bottomPanel = new JPanel();
  protected VerticalFlowLayout	verticalFlowLayout1 = new VerticalFlowLayout();
  protected JPanel		buttonPanel = new JPanel();
  protected JSeparator		jSeparator1 = new JSeparator();
  protected FlowLayout		flowLayout3 = new FlowLayout();
  protected VerticalFlowLayout	verticalFlowLayout2 = new VerticalFlowLayout();
  protected JFrame		parent;
  protected ImageIcon		waitImage;
  protected String              title;
  protected String              message;

  public SimpleWaitDialog( JFrame parent, String title, String message )
  {
    super( parent );

    this.parent   = parent;
    this.title = title;
    this.message = message;
    initialize();
  }

  private void initialize()
  {
    enableEvents( AWTEvent.WINDOW_EVENT_MASK );
    try
    {
      jbInit();
      pack();
    }
    catch( Exception ex )
    {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception
  {
    this.setTitle( this.title );
    this.setModal( true );

    waitImage = ImageMgr.getIcon( ImageMgr.WAIT_ICON );

    mainPanel.setLayout( borderLayout1 );
    topPanel.setLayout( flowLayout1 );
    topLabel.setFont( new java.awt.Font( "Dialog", Font.BOLD, 16 ) );
    topLabel.setForeground( Color.blue );
    topLabel.setText( this.message );
    notePanel.setLayout( verticalFlowLayout2 );
    bottomPanel.setLayout( verticalFlowLayout1 );
    buttonPanel.setLayout( flowLayout3 );
    noteLabel.setHorizontalAlignment( SwingConstants.CENTER );
    noteLabel.setHorizontalTextPosition( SwingConstants.CENTER );
    noteLabel.setFont( new java.awt.Font( "Dialog", Font.PLAIN, 12 ) );
    noteLabel.setForeground( Color.black );
    noteLabel.setText( "                    " );
    verticalFlowLayout2.setAlignment( VerticalFlowLayout.MIDDLE );
    gifLabel = new JLabel( waitImage );
    gifLabel.setHorizontalAlignment( SwingConstants.CENTER );
    gifLabel.setHorizontalTextPosition( SwingConstants.CENTER );
    getContentPane().add( mainPanel );
    mainPanel.add( topPanel, BorderLayout.NORTH );
    topPanel.add( topLabel, null );
    mainPanel.add( notePanel, BorderLayout.CENTER );
    notePanel.add( noteLabel, null );
    notePanel.add( gifLabel, null );
    mainPanel.add( bottomPanel, BorderLayout.SOUTH );
    bottomPanel.add( jSeparator1, null );
    bottomPanel.add( buttonPanel, null );

    Dimension frmSize;
    Point loc;
    Dimension dlgSize = getPreferredSize();
    if( parent != null )
    {
      frmSize = parent.getSize();
      loc = parent.getLocation();
    }
    else
    {
      frmSize = Toolkit.getDefaultToolkit().getScreenSize();
      loc = new Point();
    }
    setLocation( ( frmSize.width - dlgSize.width ) / 2 + loc.x,
                 ( frmSize.height - dlgSize.height ) / 2 + loc.y );
  }

  protected void processWindowEvent( WindowEvent e )
  {
    if( e.getID() == WindowEvent.WINDOW_CLOSING )
    {
      super.dispose();
    }
    super.processWindowEvent( e );
  }
}